package org.example.video.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.R;
import org.example.commonapi.util.oss.OssService;
import org.example.commonapi.domain.Video;
import org.example.commonapi.vo.VideoHistoryVO;
import org.example.video.service.VideoService;
import org.example.commonapi.vo.VideoVO;
import org.example.video.strategy.VideoSortType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/video")
@Slf4j
public class VideoController {

    @Resource
    private VideoService videoService;

    @Resource
    private OssService ossService;

    /**
     * 获取热门视频
     * @return
     */
    @GetMapping("/getHotVideoList")
    public R<List<VideoVO>> getHotVideoList() {
        return videoService.getHotVideoList();
    }

    /**
     * 根据指定学科获取对应的视频列表
     * @param subject 指定学科
     * @return
     */
    @GetMapping("/getTargetSubjectVideoList/{subject}")
    public R<List<VideoVO>> getVideosBySubject(
            @PathVariable String subject,
            @RequestParam(required = false, defaultValue = "PLAY_COUNT") VideoSortType sortType) {
        return videoService.getTargetSubjectVideoList(subject, sortType);
    }

    /**
     * 点击视频后要进行的更新操作 一个是更新当前用户对视频标签的点击量 一个是视频的点击量 同时返回对应视频
     * @param request
     * @param videoId
     * @return
     */
    @PutMapping("/clickVideo")
    public R<Video> clickVideo(HttpServletRequest request, @RequestParam Integer videoId) {
        return videoService.clickVideo(request, videoId);
    }

    /**
     * 视频上传文件接口
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public R<String> uploadFile(@RequestPart("file") MultipartFile file) {
        String uploadAddress = ossService.upload(file);
        System.out.println("链接" + uploadAddress);
        return R.success(uploadAddress);
    }


    /**
     * 添加视频接口
     * @param video
     * @return
     */
    @PostMapping("/addVideo")
    public R addVideo(HttpServletRequest request,@RequestBody Video video) {
        return videoService.addVideo(request,video);
    }

    /**
     * 新增一条视频收藏记录/已存在就删除一条视频收藏记录
     * @param request
     * @param videoId
     * @return
     */
    @PostMapping("/collectVideo")
    public R collectVideo(HttpServletRequest request,@RequestParam Integer videoId) {
        return videoService.collectVideo(request,videoId);
    }

    /**
     * 获取一个视频是否被当前用户收藏
     * @param request
     * @param videoId
     * @return
     */
    @GetMapping("/getVideoCollectState")
    public R<Boolean> getVideoCollectState(HttpServletRequest request,@RequestParam Integer videoId) {
        return videoService.getVideoCollectState(request,videoId);
    }

    /**
     * 学生获取自己收藏的视频
     * @param request
     * @return
     */
    @GetMapping("/getMyCollectVideo")
    public R<List<VideoVO>> getMyCollectVideo(HttpServletRequest request) {
        return videoService.getMyCollectVideo(request);
    }

    /**
     * 教师获取自己上传的视频
     * @param request
     * @return
     */
    @GetMapping("/getMyVideo")
    public R<List<Video>> getMyVideo(HttpServletRequest request) {
        return videoService.getMyVideo(request);
    }

    /**
     * 教师根据视频标题查询指定视频
     * @param title
     * @return
     */
    @GetMapping("/searchVideoByTitle")
    public R<List<Video>> searchVideoByTitle(@RequestParam String title) {
        return videoService.searchVideoByTitle(title);
    }

    /**
     * 根据视频id获取视频信息
     * @param videoId
     * @return
     */
    @GetMapping("/getVideoInfoById")
    public R<Video> getVideoInfoById(@RequestParam Integer videoId) {
        return videoService.getVideoInfoById(videoId);
    }

    /**
     * 获取用户观看视频的历史记录
     * @param request
     * @return
     */
    @GetMapping("/getVideoHistory")
    public R<List<VideoHistoryVO>> getVideoHistory(HttpServletRequest request) {
        return videoService.getVideoHistory(request);
    }

    /**
     * 获取教师最近7天收藏量和播放量 list[0] 7日播放量 list[1] 7日收藏量
     * @param request
     * @return
     */
    @GetMapping("/getRecentDayVideoDetail")
    public R<List<List<Integer>>> getRecentDayVideoDetail(HttpServletRequest request) {
        return videoService.getRecentDayVideoDetail(request);
    }


}
